<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" formType="edit" labelWidth="100" :cols="3" :isSubmitBtn="false" @formChange="formChange" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog()">关 闭</el-button>
      <AjaxButton :click="saveDialog" type="primary">保 存</AjaxButton>
    </div>
  </div>
</template>

<script>
import { editWorkHourPrice } from '@/api/repair/repair/workHourPrice/index';
// import { findProjectModelList } from '@/api/repair/repair/workHourPrice/index';
import { getChexSelectTable } from '@/api/repair/repairReception/customer/brand.js'; // 车系

export default {
  props: ['selectColumn', 'excloudSeries'],
  data() {
    return {
      formList: this.createFormList(),
      saveData: null,
      seriesCodeValue: ''
    };
  },

  mounted() {
    this.getChexSelect();
    this.getData();
  },
  methods: {
    // 创建新增表单数据
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '工时单价代码',
          fieldName: 'labourPriceCode',
          disabled: true,
          style: { width: '150px' },
          rules: [
            { required: true, message: '工时单价代码', trigger: 'blur' },
            { min: 0, max: 10, pattern: /^[a-zA-Z0-9]+$/, message: '只允许输入数字和字母' }
          ]
        },
        {
          type: 'INPUT',
          label: '工时单价',
          fieldName: 'labourPrice',
          placeholder: '工时单价',
          maxlength: 8,
          style: { width: '150px' },
          rules: [
            { required: true, message: '请输入工时单价', trigger: 'blur' },
            { min: 0, max: 8, pattern: /(^\d+(\.)?(\d)?(\d)?$)/, message: '填写数字,最多2位小数' }
          ]
        },
        {
          type: 'INPUT',
          label: '工时名称',
          fieldName: 'labourPriceName',
          placeholder: '工时名称',
          maxlength: 50,
          initialValue: '',
          style: { width: '150px' },
          rules: [{ required: true, message: '请输入工时名称', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'seriesCode',
          placeholder: '车系',
          style: { width: '150px' },
          itemList: [],
          initialValue: '',
          rules: [{ required: true, message: '请选择车系', trigger: 'change' }],
          disabled: true
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'isValid',
          placeholder: '请选择',
          style: { width: '150px' },
          itemList: [
            { value: 10041001, text: '启用' },
            { value: 10041002, text: '未启用' }
          ],
          rules: [{ required: true, message: '请选择状态', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '是否在维修项目中启用',
          fieldName: 'isItemUse',
          placeholder: '请选择',
          style: { width: '150px' },
          itemList: [
            { value: 10041001, text: '启用' },
            { value: 10041002, text: '未启用' }
          ],
          rules: [{ required: true, message: '请选择状态', trigger: 'change' }],
          change: val => {
            if (val == 10041001) {
              this.$alert('启用工时单价定义将会重新计算并替换该车系下所有的维修项目的工时费，请确认！', '提示！', {
                confirmButtonText: '确定'
              });
            }
          }
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'labourRemark',
          placeholder: '备注',
          initialValue: '',
          style: { width: '150' },
          rules: [{ message: '请输入备注', trigger: 'blur' }]
        }
      ];
    },
    // 表单提交触发事件
    formChange(saveData) {
      this.saveData = saveData;
    },
    // 数据带入
    async getData() {
      this.formList.find(x => x.fieldName === 'labourPriceCode').initialValue = this.selectColumn.LABOUR_PRICE_CODE;
      this.formList.find(x => x.fieldName === 'labourPrice').initialValue = this.selectColumn.LABOUR_PRICE;
      // let queryParams = { isSeriesId: '1' };
      // this.formList.find(x => x.fieldName === 'seriesCode').itemList = (await findProjectModelList(queryParams)).data.map(x => ({ text: x.MODEL_LABOUR_NAME, value: x.MODEL_LABOUR_CODE + '' }));
      this.formList.find(x => x.fieldName === 'labourPriceName').initialValue = this.selectColumn.LABOUR_PRICE_NAME;

      // let seriesList = this.formList.find(x => x.fieldName === 'seriesCode').itemList;
      this.formList.find(x => x.fieldName === 'seriesCode').initialValue = this.selectColumn.series_code;
      this.seriesCodeValue = this.selectColumn.series_code;
      this.formList.find(x => x.fieldName === 'isValid').initialValue = this.selectColumn.STATUS;
      this.formList.find(x => x.fieldName === 'labourRemark').initialValue = this.selectColumn.REMARK;
      if (!this.selectColumn.IS_ITEM_USE) {
        this.selectColumn.IS_ITEM_USE = 10041002;
      }
      this.formList.find(x => x.fieldName === 'isItemUse').initialValue = this.selectColumn.IS_ITEM_USE;
    },
    // 保存编辑信息
    async saveDialog() {
      // this.saveData = null;
      this.$refs.formPanel.SUBMIT_FORM();
      // 重置车系code
      this.saveData.seriesCode && (this.saveData.seriesCode = this.seriesCodeValue);
      if (this.saveData !== null) {
        let res = await editWorkHourPrice(this.saveData);
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        } else {
          this.closeDialog();
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', param);
    },
    async getChexSelect() {
      // let queryParams = { isSeriesId: '1' };
      const projectModelList = (await getChexSelectTable()).data.map(x => ({ value: x.seriesCode + '', text: x.seriesName }));
      let that = this;
      const result = projectModelList.filter(function(v) {
        return !that.excloudSeries.some(val => {
          return val.SERIES_CODE == v.value;
        });
      });
      let seriesCode = this.formList.find(x => x.fieldName === 'seriesCode');
      let item = projectModelList.find(x => x.value == seriesCode.initialValue);
      seriesCode.initialValue = item.text;
      this.seriesCodeValue = item.value;
      seriesCode.itemList = result;
    }
  }
};
</script>

<style></style>
